/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author KeaneDCunha
 */
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Listener {

    public static void main(String[] args) throws IOException
    {
        ServerSocket peerSocket = null;
        boolean isListening = true;
        String peerHostName = null;
        //int peerPort = -1;
        int myPort = -1;
        String myName = null;

        try
        {
            if (args.length != 3)
            {
                System.err.println("ERROR: Invalid arguments!");
                System.exit(-1);
            }
        

            peerHostName = args[0];
            //peerPort = Integer.parseInt(args[1]);
            myPort = Integer.parseInt(args[1]);
            myName = args[2];
            peerSocket = new ServerSocket(myPort);

        }
        catch (NumberFormatException nfe) {
            System.err.println("ERROR: Invalid port number(s)!");
            System.exit(-1);
        }
        catch (IOException e) {
            System.err.println("ERROR: Could not listen on port!");
            System.exit(-1);
        }

        while (isListening)
        {
            new ListenerThreadHandler(peerSocket.accept(), myPort).start();
        }

        peerSocket.close();

    }

}
